import Vue from 'vue';
import Vuex from 'vuex';
// import { login,regist,logout } from '@/api/login'
import { getToken,setToken } from '@/utils/auth'

Vue.use(Vuex);
import { MessageBox } from 'mint-ui';
export default new Vuex.Store({

  state: {
    goodsList:[],
    carts: {
      cartTotal:{

      },
      cartList:[],
    },
    newGoodsList: [],
    channel: [],
    grouponList: [],
    banner: [],
    brandList: [],
    hotGoodsList: [],
    topicList: [],
    floorGoodsList: [],
    categoryIndexList:[]
    ,username:'',
    token:''
  },
  mutations: {
    setGoodsList(state, data) {
      state.goodsList=data
    },
    /*setHotGoodsList(state, data) {
      state.hotGoodsList = data;
    },*/
    setData(state, data) {
      state.newGoodsList = data.newGoodsList
      state.channel = data.channel
      state.grouponList = data.grouponList
      state.banner = data.banner
      state.brandList = data.brandList
      state.hotGoodsList = data.hotGoodsList
      state.topicList = data.topicList
      state.floorGoodsList = data.floorGoodsList
    },
    setCategoryIndexList(state, data) {
      state.categoryIndexList=data
    },
    setSubMenus(state, data) {
      state.categoryIndexList.currentSubCategory=data.brotherCategory
      state.categoryIndexList.currentCategory=data.parentCategory
    }
    ,
    SET_NAME: (state, username) => {
      state.username = username;
    },
    SET_TOKEN: (state, token) => {
      state.token = token;
      setToken(token);
    },
    //购物车删除
    shanchu:(state,index)=>{
      MessageBox.confirm('确定删除该商品么？').then(action=>{
        state.carts.cartList.splice(index,1)
        // localStorage.setItem("carts",JSON.stringify(state.carts));
      })
    } ,
    //数量加
    add(state,index){
      state.carts.cartList[index].number++
    },
    //数量减
    reduce(state,index){
      state.carts.cartList[index].number==1? state.carts.cartList[index].number=1:  state.carts.cartList[index].number--
    },
  },
  actions: {
    // commitHotGoodsList: ({commit}, data) => commit('setHotGoodsList', data),
    comitData: ({commit}, data) => commit('setData', data),
    comitCategoryIndexList: ({commit},data) => commit('setCategoryIndexList',data),
    commitSubMenus: ({commit}, data)=> commit('setSubMenus',data),

    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim();
      const password = userInfo.password.trim();
      return new Promise((resolve, reject) => {
        login(username, password).then(response => {
          const data = response.data;
          commit('SET_NAME', data.username);
          commit('SET_TOKEN', data.token);
          setUsername(data.username);
          setToken(data.token);
          resolve(response); }).catch(error => { reject(error) }) }) },
    // 注册
    Regist({ commit }, userInfo) {
      const username= userInfo.username.trim();
      const password = userInfo.password.trim();
      return new Promise((resolve, reject) => {
        regist(username, password).then(response => {
          const data = response.data;
          commit('SET_NAME', data.username);
          commit('SET_TOKEN', data.token);
          setUsername(data.username);
          setToken(data.token);
          resolve(response);
        }).catch(error => {
          reject(error) }) }) },
    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout().then(response => {
          commit('SET_USERNAME', '');
          commit('SET_TOKEN', '');
          setUsername('');
          setToken(false);
          //removeName();
          //removeToken();
          resolve(response);
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        setToken(false);
        commit('SET_TOKEN', false);
        resolve()
      })
    }
  }


});
